<template>
  <div>
    <strong style="float:left">请修改个人信息：</strong>
    <el-radio-group v-model="labelPosition" size="small">
      <el-radio-button label="left">左对齐</el-radio-button>
      <el-radio-button label="right">右对齐</el-radio-button>
      <el-radio-button label="top">顶部对齐</el-radio-button>
    </el-radio-group>
    <div style="margin: 20px;"></div>
    <el-form :label-position="labelPosition" label-width="100px">
      <el-form-item label="用户名">
        <el-input v-model="userSO_json.userName" disabled></el-input>
      </el-form-item>
      <el-form-item label="身份">
        <el-input v-model="userSO_json.role" disabled></el-input>
      </el-form-item>
      <el-form-item label="联系方式">
        <el-input v-model="userSO_json.mobilePhone" disabled></el-input>
      </el-form-item>
      <el-form-item label="旧密码">
        <el-input v-model="userSO_json.passWord" disabled></el-input>
      </el-form-item>
      <el-form-item label="请输入新密码">
        <el-row :gutter="0">
          <el-col :span="12">
            <el-input v-model="newPassWord1" placeholder="请输入您脑海中的密码"></el-input>
          </el-col>
          <el-col :span="12">
            <i class="el-icon-question" style="font-size:10px" v-if="newPassWord1==''">
              <strong>请至少6-10个字符，至少1个大写字母，1个小写字母和1个数字，其他可以是任意字符,且不与旧密码重复</strong>
            </i>
            <i class="el-icon-success" style="font-size:40px" v-else-if="rightPsw1"></i>
            <i class="el-icon-error" style="font-size:40px" v-else-if="!rightPsw1"></i>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="再次确认密码">
        <el-row :gutter="0">
          <el-col :span="12">
            <el-input v-model="newPassWord2" placeholder="请再次确认您的密码"></el-input>
          </el-col>
          <el-col :span="12">
            <i class="el-icon-question" style="font-size:10px" v-if="newPassWord2==''">
              <strong>重新输入您的新密码，防止手误操作</strong>
            </i>
            <i class="el-icon-success" style="font-size:40px" v-else-if="rightPsw2"></i>
            <i class="el-icon-error" style="font-size:40px" v-else-if="!rightPsw2"></i>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="confirmDialog=true">确认</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="再次确认修改您的密码" :visible.sync="confirmDialog">
      <strong>确认修改你的新密码为{{newPassWord2}},请牢记</strong>
      <div slot="footer" class="dialog-footer">
        <el-button @click="confirmDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirmDialog = false;finalUpdate(newPassWord2)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      labelPosition: "right",
      newPassWord1: "",
      newPassWord2: "",
      confirmDialog: false
    };
  },
  created() {},
  computed: {
    //进入的用户
    userSO_json() {
      let userSO_json = JSON.parse(sessionStorage.getItem("userSO_json"));
      return userSO_json;
    },
    //不能和旧密码相同，且符合正则表达式
    rightPsw1() {
      //至少6-10个字符，至少1个大写字母，1个小写字母和1个数字，其他可以是任意字符：
      if (this.newPassWord1 != this.userSO_json.passWord) {
        return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{6,10}$/.test(
          this.newPassWord1
        );
      } else {
        return 0;
      }
    },
    //重复输入
    rightPsw2() {
      //至少6-10个字符，至少1个大写字母，1个小写字母和1个数字，其他可以是任意字符：
      if (this.newPassWord1 == this.newPassWord2) {
        return 1;
      } else {
        return 0;
      }
    }
  },
  methods: {
    finalUpdate(val) {
      axios
        .post(
          "/api/UserController/user/password?passWord=" + val,
          this.userSO_json,
          {
            headers: {
              //头部信息
              "Content-Type": "application/json;charset=utf-8",
              token: this.userSO_json.token
            }
          }
        )
        .then(response => {
          this.$message({
            showClose: true,
            message: "您已经成功的修改了密码，请重新登录",
            type: "warning"
          });
        })
        .catch(error => {
          this.$message({
            showClose: true,
            message: "后端token过期，请重新登录",
            type: "warning"
          });
        });
    }
  }
};
</script>